package com.edu.dreams.controller;

import com.edu.dreams.entity.Anim;
import com.edu.dreams.entity.User;
import com.edu.dreams.mapper.AnimMapper;
import com.edu.dreams.mapper.NewsMapper;
import com.edu.dreams.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

//@Controller注解
@Controller

public class MainController {

	@Autowired
	AnimMapper animMapper;

	@Autowired
	NewsMapper newsMapper;

	@Autowired
	UserMapper userMapper;

	// 向首页跳转
	@RequestMapping("/")
	public String index() {
		return "index";
	}

	// 向登录页面跳转
	@RequestMapping("login")
	public String login() {
		return "login";
	}

	@RequestMapping("home")
	public String home(@RequestParam(required = false, defaultValue = "1") int startPage,
			@RequestParam(required = false, defaultValue = "3") int pagesize, Model model) {
		PageHelper.startPage(startPage, pagesize);
		PageInfo<Anim> datas = new PageInfo<>(animMapper.getAllAnim());
		model.addAttribute("pageNumNow", startPage);
		model.addAttribute("datas", datas);
		model.addAttribute("animdata", animMapper.getAllAnim());
		return "home";
	}

	// 向管理员页面跳转
	@RequestMapping("adminhome")
	public String adminhome() {
		return "adminhome";
	}

	// 向数据展示页面跳转
	@RequestMapping("admin")
	public String admin() {
		return "admin";
	}

	@RequestMapping("personal")
	public String personal(Model model, HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		model.addAttribute("info", userMapper.getUserInfoById(user.getUser_id()));
		model.addAttribute("shouchang", animMapper.getUserShouCang(user.getUser_id()));
		model.addAttribute("lishi", animMapper.getUserLiShi(user.getUser_id()));
		return "personal";
	}

	@RequestMapping("updatepersonalpage")
	public String updatepersonalpage(int id, Model model, HttpServletRequest request) {
		User user = (User) request.getSession().getAttribute("user");
		model.addAttribute("info", userMapper.getUserInfoById(user.getUser_id()));
		return "update_personal";
	}

	@RequestMapping("logout")
	@ResponseBody
	public String logout(HttpServletRequest request) {
		request.getSession().invalidate();
		return "<script>alert('退出成功！');window.location.href='" + request.getContextPath() + "/';</script>";
	}

	@RequestMapping("rank")
	public String rank(Model model) {
		model.addAttribute("animlist", animMapper.sortAnim());
		return "rank";
	}

	@RequestMapping("search")
	public String search(Model model, String aname, Integer type) {
		model.addAttribute("searchtext", aname);
		model.addAttribute("type", type);
		if (type == 1) {
			model.addAttribute("animlist", animMapper.searchAnim(aname));
		} else if (type == 2) {
			model.addAttribute("newslist", newsMapper.searchNews(aname));
		}
		return "search";
	}
}
